package com.magic.item.controller;

import com.github.pagehelper.PageInfo;
import com.magic.item.pojo.Order;
import com.magic.item.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lan.jianyuan
 * @create 2021-03-24-17:24
 */
@Controller
public class OrdersController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("order/page")
    public ResponseEntity<PageInfo<Order>> getPageInfo(
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        PageInfo<Order> pageInfo = orderService.getPageInfo(keyword, pageNum, pageSize);
        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getList())) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageInfo);
    }
    @GetMapping("order/{oid}")
    public ResponseEntity<List<Order>> queryOrderByUid(@PathVariable("oid") Long oid) {
        // 执行查询操作
        List<Order> userList = this.orderService.queryOrderById(oid);
        if (CollectionUtils.isEmpty(userList)) {
            return ResponseEntity.notFound().build();
        }
        // 响应200
        return ResponseEntity.ok(userList);
    }
    @PutMapping("order/update")
    public ResponseEntity<Void> updateOrder(@RequestBody Order order) {
        orderService.updateOrder(order);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
